package com.tecnoagrosoft.zafra.control.utils;

import java.util.HashMap;
import java.util.List;



import android.content.Context;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;


public abstract class ExpandableListAdapter extends BaseExpandableListAdapter {

	private Context contexto;
	private int R_layout_IdViewHeader;
	private int R_layout_IdViewChildren;

	private List<String> listDataHeader; // header titles
	// child data in format of header title, child title
	private HashMap<String, List<?>> listDataChild;

	public ExpandableListAdapter(Context context, int R_layout_IdViewHeader,
			int R_layout_IdViewChildren, List<String> listDataHeader,
			HashMap<String, List<?>> listChildData) {
		this.contexto = context;
		this.listDataHeader = listDataHeader;
		this.listDataChild = listChildData;
		this.R_layout_IdViewHeader = R_layout_IdViewHeader;
		this.R_layout_IdViewChildren = R_layout_IdViewChildren;
	}

	@Override
	public Object getChild(int groupPosition, int childPosititon) {
		return this.listDataChild.get(this.listDataHeader.get(groupPosition)).get(childPosititon);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, final int childPosition,
			boolean isLastChild, View view, ViewGroup parent) {

		// final String childText = (String) getChild(groupPosition,
		// childPosition);

		// if (view == null) {
		// LayoutInflater infalInflater = (LayoutInflater) this.contexto
		// .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		// view = infalInflater.inflate(R.layout.list_item, null);
		// }
		//
		// TextView txtListChild = (TextView) view
		// .findViewById(R.id.lblListItem);
		//
		// txtListChild.setText(childText);
		// return view;

		if (view == null) {
			LayoutInflater vi = (LayoutInflater) contexto.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = vi.inflate(R_layout_IdViewChildren, null);
		}
		onCreateChinldren( getChild(groupPosition,childPosition), view);
		return view;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return this.listDataChild.get(this.listDataHeader.get(groupPosition))
				.size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return this.listDataHeader.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return this.listDataHeader.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View view,
			ViewGroup parent) {

		// String headerTitle = (String) getGroup(groupPosition);
		if (view == null) {
			LayoutInflater infalInflater = (LayoutInflater) this.contexto
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = infalInflater.inflate(R_layout_IdViewHeader, null);
		}

		// TextView lblListHeader = (TextView)
		// view.findViewById(R.id.lblListHeader);
		// lblListHeader.setTypeface(null, Typeface.BOLD);
		// lblListHeader.setText(headerTitle);

		onCreateHeader(listDataHeader.get(groupPosition), view);

		return view;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	public abstract void onCreateHeader(Object entrada, View view);

	public abstract void onCreateChinldren(Object entradChildlen, View view);
}